package com.cindata

import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

@Transactional
@Secured(['isAuthenticated()'])
class BatchEstateController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) 
    {
        def batch = params['batch']
        def sourceAddress = params['sourceAddress']
        def status = params['status']
        String sql = " from BatchEstate e where e.batch = ${batch}"
        if (sourceAddress)
        {
            sql += " and e.estate.sourceAddress like '%${sourceAddress}%'"
        }
        if (status) 
        {
            sql += " and e.estate.estateStatus = ${status}"
        }
        def countSql = "select count(e.id) " + sql
        sql += " order by e.modifiedDate desc "

        def batchEstateList = BatchEstate.executeQuery(sql, [max: params.max ?: 10, offset: params.offset ?: 0])
        def batchEstateCount = BatchEstate.executeQuery(countSql)

        respond BatchEstate, model: [batchEstateList: batchEstateList, batchEstateCount: batchEstateCount[0]]
    }

    def show(BatchEstate batchEstate) {
        respond batchEstate
    }

    def create() {
        respond new BatchEstate(params)
    }

    @Transactional
    def save(BatchEstate batchEstate) {
        if (batchEstate == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (batchEstate.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond batchEstate.errors, view:'create'
            return
        }

        batchEstate.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'batchEstate.label', default: 'BatchEstate'), batchEstate.id])
                redirect batchEstate
            }
            '*' { respond batchEstate, [status: CREATED] }
        }
    }

    def edit(BatchEstate batchEstate) {
        respond batchEstate
    }

    @Transactional
    def update(BatchEstate batchEstate) {
        if (batchEstate == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (batchEstate.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond batchEstate.errors, view:'edit'
            return
        }

        batchEstate.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'batchEstate.label', default: 'BatchEstate'), batchEstate.id])
                redirect batchEstate
            }
            '*'{ respond batchEstate, [status: OK] }
        }
    }

    @Transactional
    def delete(BatchEstate batchEstate) {

        if (batchEstate == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        batchEstate.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'batchEstate.label', default: 'BatchEstate'), batchEstate.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'batchEstate.label', default: 'BatchEstate'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
